import 'dart:async';
import 'dart:collection';

import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:sptapp/api/interface/home_api.dart';
import 'package:sptapp/api/request/response_data.dart';
import 'package:sptapp/moudle/message.dart';
import 'package:sptapp/page/monitor/inverter_detail_dialog.dart';
import 'package:sptapp/widget/common_widget.dart';

import '../../config/const.dart';
import '../../moudle/common_info.dart';
import '../../moudle/event_bus.dart';
import '../../moudle/permiss_handler/permission_operate.dart';
import '../../moudle/refresh/my_refresh.dart';
import '../../widget/top_device_select.dart';
import '../home/home.dart';
import '../index_drawer_page.dart';

class InverterDetailPage extends StatefulWidget {
  // final String sn;
  const InverterDetailPage({super.key,});

  @override
  State<InverterDetailPage> createState() => _InverterDetailPageState();
}

class _InverterDetailPageState extends State<InverterDetailPage> with SingleTickerProviderStateMixin,AutomaticKeepAliveClientMixin {

  Map detailInfo = {};
  List _status = [
    {
      'name':monitor_detail_page_status_tx.tr,
      'status':'1',
      'value':monitor_detail_page_status_tx_online.tr,
      'ofvalue':monitor_detail_page_status_tx_offline.tr,
      'unvalue':monitor_detail_page_status_tx_unbind.tr,
      'code':'TXZT',
      'desc':'Is the device online?'
    },
    {
      'name':monitor_detail_page_status_work.tr,
      'status':'1',
      'value':monitor_detail_page_status_work_normal.tr,
      'ofvalue':monitor_detail_page_status_work_error.tr,
      'code':'GZZT',
      'desc':'Is the device functioning properly? Are there any error messages?'
    },
    {
      'name':monitor_detail_page_status_bw.tr,
      'status':'1',
      'value':monitor_detail_page_status_bw_normal.tr,
      'ofvalue':monitor_detail_page_status_bw_error.tr,
      'code':'BWZT',
      'desc':'Is the device connected to the power grid?'
    },
    // {
    //   'name':monitor_detail_page_status_gz.tr,
    //   'status':'0',
    //   'value':monitor_detail_page_status_gz_normal.tr,
    //   'ofvalue':monitor_detail_page_status_gz_error.tr,
    //   'code':'GZZT',
    //   'desc':''
    // }
  ];
  List _newDatas = [];

  bool isOpen = false;
  bool isDone = true;

  late StreamSubscription _devicesubscription;
  late StreamSubscription _Slidersubscription;
  late StreamSubscription _Timersubscription;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // _tabController = TabController(length: 2, vsync: this);
    _devicesubscription = eventBus.on<DeviceInfoChange>().listen((val){
      _getList(type: 1);
    });
    _Slidersubscription = eventBus.on<IndexSliderChange>().listen((val){
      setState(() {
        isOpen = val.isOpen;
      });
    });
    _Timersubscription = eventBus.on<IndexRefreshTimer>().listen((val){
      _getList();
    });
   _getList(type: 1);
  }

  _showNoDeviceAlert(){
    SmartDialog.show(builder: (ctx){
      return Container(
        height: 530.w,
        padding: EdgeInsets.only(left: 48.w,right: 48.w,top: 32.w,bottom: 48.w),
        decoration: BoxDecoration(
            color: Color(0xff1E1E1E),
            borderRadius: BorderRadius.only(topLeft: Radius.circular(32.w),topRight: Radius.circular(32.w))
        ),
        child: Column(
          children: [
            Container(
              child: Row(
                children: [
                  Text('Add new device',style: TextStyle(
                      color: Colors.white,
                      fontSize: 28.sp,
                      fontWeight: FontWeight.w600
                  ),),
                  GestureDetector(
                    onTap: (){
                      SmartDialog.dismiss();
                    },
                    child: Icon(Icons.close,size: 30.sp,color: Color(0xff999999),),
                  )
                ],
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
              ),
            ),
            SizedBox(height: 100.w,),
            GestureDetector(
              onTap: (){
                SmartDialog.dismiss();
                getCameraPermission(context, (){
                  Navigator.of(context).pushNamed('ScanPage').then((val){
                    if(mounted){setState(() {});}
                  });
                });
              },
              child: Container(
                height: 84.w,
                width: 654.w,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    color: themeColor,
                    borderRadius: BorderRadius.circular(8.w)
                ),
                child:Text('Scan Your Device',style: TextStyle(
                    color: Colors.white,
                    fontSize: 28.sp,
                    fontWeight: FontWeight.w600
                ),),
              ),
            ),
            SizedBox(height: 32.w,),
            GestureDetector(
              onTap: (){
                SmartDialog.dismiss();
                Navigator.of(context).pushNamed('MinAddDeviceInput').then((val){
                  if(mounted){setState(() {});}
                });
              },
              child: Container(
                height: 84.w,
                width: 654.w,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    color: Color(0xff2A2A2A),
                    borderRadius: BorderRadius.circular(8.w)
                ),
                child:Text('Manual Entry'.tr,style: TextStyle(
                    color: Colors.white,
                    fontSize: 28.sp,
                    fontWeight: FontWeight.w600
                ),),
              ),
            ),
          ],
        ),
      );
    },alignment: Alignment.bottomCenter);
    // SmartDialog.show(
    //     alignment: Alignment.center,
    //     builder: (BuildContext ctx){
    //       return Container(
    //         decoration: BoxDecoration(
    //             color: Colors.white,
    //             borderRadius: BorderRadius.circular(20.w)
    //         ),
    //         margin: EdgeInsets.symmetric(horizontal: 30.w),
    //         padding: EdgeInsets.symmetric(horizontal: 30.w,vertical: 30.w),
    //         child: Container(
    //           child: Column(
    //             mainAxisSize: MainAxisSize.min,
    //             children: [
    //               Container(
    //                 child: Text(alert_waring_title.tr),
    //               ),
    //               SizedBox(height: 30.w,),
    //               Container(
    //                 child: Text(home_have_no_device_alert_text.tr),
    //               ),
    //               SizedBox(height: 30.w,),
    //               Container(
    //                 child: Row(
    //                   mainAxisAlignment: MainAxisAlignment.spaceAround,
    //                   children: [
    //                     GestureDetector(
    //                       onTap: (){
    //                         SmartDialog.dismiss();
    //                       },
    //                       child: Container(
    //                         width: 200.w,
    //                         height: 90.w,
    //                         alignment: Alignment.center,
    //                         decoration: BoxDecoration(
    //                             color: Colors.grey,
    //                             borderRadius: BorderRadius.circular(20.w)
    //                         ),
    //                         child: Text(alert_waring_canclebtn_text.tr),
    //                       ),
    //                     ),
    //                     GestureDetector(
    //                       onTap: (){
    //                         SmartDialog.dismiss();
    //                         Navigator.of(context).pushNamed('AddDevicePage').then((val){
    //                           _getList();
    //                         });
    //                       },
    //                       child: Container(
    //                         width: 200.w,
    //                         height: 90.w,
    //                         alignment: Alignment.center,
    //                         decoration: BoxDecoration(
    //                             color: themeColor,
    //                             borderRadius: BorderRadius.circular(20.w)
    //                         ),
    //                         child: Text(alert_waring_surebtn_text.tr),
    //                       ),
    //                     )
    //                   ],
    //                 ),
    //               )
    //             ],
    //           ),
    //         ),
    //       );
    //     }
    // );
  }

  _showNoNetworkAlert(){
    SmartDialog.show(builder: (ctx){
      return Container(
        height: 530.w,
        padding: EdgeInsets.only(left: 48.w,right: 48.w,top: 32.w,bottom: 48.w),
        decoration: BoxDecoration(
            color: Color(0xff1E1E1E),
            borderRadius: BorderRadius.only(topLeft: Radius.circular(32.w),topRight: Radius.circular(32.w))
        ),
        child: Column(
          children: [
            Container(
              child: Row(
                children: [
                  Text('Device Network Settings',style: TextStyle(
                      color: Colors.white,
                      fontSize: 28.sp,
                      fontWeight: FontWeight.w600
                  ),),
                  GestureDetector(
                    onTap: (){
                      SmartDialog.dismiss();
                    },
                    child: Icon(Icons.close,size: 30.sp,color: Color(0xff999999),),
                  )
                ],
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
              ),
            ),
            SizedBox(height: 100.w,),
            GestureDetector(
              onTap: (){
                SmartDialog.dismiss();
                Navigator.of(context).pushNamed('DeviceNetPage').then((val){
                  if(mounted){setState(() {});}
                });
              },
              child: Container(
                height: 84.w,
                width: 654.w,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    color: themeColor,
                    borderRadius: BorderRadius.circular(8.w)
                ),
                child:Text('Select network',style: TextStyle(
                    color: Colors.white,
                    fontSize: 28.sp,
                    fontWeight: FontWeight.w600
                ),),
              ),
            ),
            SizedBox(height: 32.w,),
            GestureDetector(
              onTap: (){
                SmartDialog.dismiss();
                Navigator.of(context).pushNamed('DeviceNetPage').then((val){
                  if(mounted){setState(() {});}
                });
              },
              child: Container(
                height: 84.w,
                width: 654.w,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    color: Color(0xff2A2A2A),
                    borderRadius: BorderRadius.circular(8.w)
                ),
                child:Text('Manual Entry'.tr,style: TextStyle(
                    color: Colors.white,
                    fontSize: 28.sp,
                    fontWeight: FontWeight.w600
                ),),
              ),
            ),
          ],
        ),
      );
    },alignment: Alignment.bottomCenter);
    // SmartDialog.show(
    //     alignment: Alignment.center,
    //     builder: (BuildContext ctx){
    //       return Container(
    //         decoration: BoxDecoration(
    //             color: Colors.white,
    //             borderRadius: BorderRadius.circular(20.w)
    //         ),
    //         margin: EdgeInsets.symmetric(horizontal: 30.w),
    //         padding: EdgeInsets.symmetric(horizontal: 30.w,vertical: 30.w),
    //         child: Container(
    //           child: Column(
    //             mainAxisSize: MainAxisSize.min,
    //             children: [
    //               Container(
    //                 child: Text(alert_waring_title.tr),
    //               ),
    //               SizedBox(height: 30.w,),
    //               Container(
    //                 child: Text(home_have_no_network_alert_text.tr),
    //               ),
    //               SizedBox(height: 30.w,),
    //               Container(
    //                 child: Row(
    //                   mainAxisAlignment: MainAxisAlignment.spaceAround,
    //                   children: [
    //                     GestureDetector(
    //                       onTap: (){
    //                         SmartDialog.dismiss();
    //                       },
    //                       child: Container(
    //                         width: 200.w,
    //                         height: 90.w,
    //                         alignment: Alignment.center,
    //                         decoration: BoxDecoration(
    //                             color: Colors.grey,
    //                             borderRadius: BorderRadius.circular(20.w)
    //                         ),
    //                         child: Text(alert_waring_canclebtn_text.tr),
    //                       ),
    //                     ),
    //                     GestureDetector(
    //                       onTap: (){
    //                         SmartDialog.dismiss();
    //                         Navigator.of(context).pushNamed('DeviceNetPage').then((val){
    //                           _getList();
    //                         });
    //                       },
    //                       child: Container(
    //                         width: 200.w,
    //                         height: 90.w,
    //                         alignment: Alignment.center,
    //                         decoration: BoxDecoration(
    //                             color: themeColor,
    //                             borderRadius: BorderRadius.circular(20.w)
    //                         ),
    //                         child: Text(alert_waring_surebtn_text.tr),
    //                       ),
    //                     )
    //                   ],
    //                 ),
    //               )
    //             ],
    //           ),
    //         ),
    //       );
    //     }
    // );
  }

  _getData(){
    if(baseDevice?['status'].toString() == '2'){
      return;
    }
    // _getDeviceStatus();
    _getDeviceDatas();
    _getAllData();
    // _getEquipmentInfoData();
    // _getDeviceWaring();
  }

  _getList({int type = 0})async{
    if(type == 1){
      setState(() {
        isDone = true;
      });
    }
    getDevicesList((){
      isDone = true;
      if(baseDevices.isNotEmpty){
        _getData();
      }else{
        // _showNoDeviceAlert();
        _newDatas.clear();
      }
      if(mounted){
        setState(() {});
      }
    });
  }

  _getDeviceStatus()async{
    // ResponseData res = await HomeAPI.getEquipmentStatus({
    //   'sn':sn
    // });
    // if(res.code == 200){}
    // if(mounted){setState(() {
    // });}
  }
  _getDeviceDatas()async{
    ResponseData res = await HomeAPI.getEquipmentInfo({
      'sn':baseDeviceNum??''
    });
    if(res.code == 200 && res.data!=null){
      detailInfo = res.data;
      for(var item in _status){
        if(item['code'] == 'TXZT'){
          item['status'] = detailInfo['status'].toString();
        }else if(item['code'] == 'GZZT'){
          item['status'] = detailInfo['workingCondition'].toString();
        }else if(item['code'] == 'BWZT'){
          item['status'] = detailInfo['parallelState'].toString();
        }else if(item['code'] == 'GZZT'){
          item['status'] = detailInfo['failureState'].toString();
        }
      }
    }
    if(mounted){setState(() {
    });}
  }

  _getAllData()async{
    ResponseData res = await HomeAPI.getAllData({
      'sn':baseDeviceNum??''
    });
    if(res.code == 200 && res.data!=null && res.data is Map){
      List all = res.data['all'] ?? [];
      _newDatas.clear();
      _newDatas.addAll(all);
    }
    if(mounted){setState(() {
    });}
  }

  // _getEquipmentInfoData()async{
  //   ResponseData res = await HomeAPI.getEquipmentInfoData({
  //     'sn':baseDeviceNum??''
  //   });
  //   if(res.code == 200 && res.data!=null){
  //     infoData = res.data;
  //     List arr = res.data['devDictionaries']??[];
  //     _infos[0]['value'] = baseDeviceNum??'';
  //     for(var item in arr){
  //        if(item['code'] == 'IV'){
  //          _infos[1]['value'] = item['value'];
  //          _infos[1]['unit'] = item['unit'];
  //       }else if(item['code'] == 'WFV'){
  //          _infos[2]['value'] = item['value'];
  //          _infos[2]['unit'] = item['unit'];
  //       }else if(item['code'] == 'FOR'){
  //          _infos[3]['value'] = item['value'];
  //          _infos[3]['unit'] = item['unit'];
  //       }else if(item['code'] == 'VIR'){
  //          _infos[4]['value'] = item['value'];
  //          _infos[4]['unit'] = item['unit'];
  //       }else if(item['code'] == 'VOR'){
  //          _infos[5]['value'] = item['value'];
  //          _infos[5]['unit'] = item['unit'];
  //       }else if(item['code'] == 'CIR'){
  //          _infos[6]['value'] = item['value'];
  //          _infos[6]['unit'] = item['unit'];
  //       }else if(item['code'] == 'COR'){
  //          _infos[7]['value'] = item['value'];
  //          _infos[7]['unit'] = item['unit'];
  //       }
  //     }
  //   }
  //   if(mounted){setState(() {
  //   });}
  // }

  // _getDeviceWaring()async{
  //   ResponseData res = await HomeAPI.getMonitoringAlarm({
  //     'sn':baseDeviceNum??''
  //   });
  //   if(res.code == 200){}
  //   if(mounted){setState(() {
  //   });}
  // }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _devicesubscription.cancel();
    _Slidersubscription.cancel();
    _Timersubscription.cancel();
  }

  Widget _buildHomeNoData(int type){
    String imgurl = '';
    String title = '';
    String content = '';
    String btnTitle='';
    if(type == 1){
      imgurl = 'assets/home/home_no_device.png';
      title = 'No device added'.tr;
      content = 'Add your device to get more information.'.tr;
      btnTitle = 'Add Device'.tr;
    }else if(type == 2){
      imgurl = 'assets/home/home_no_network.png';
      title = 'Network not Connected'.tr;
      content = 'Since the network is not connected\n you won\'t be able to view the data.'.tr;
      btnTitle = 'Device Network Setting'.tr;
    }
    return Container(
      child: Column(
        children: [
          SizedBox(height: ScreenUtil().statusBarHeight+0.w,),
          _topWidget(),
          SizedBox(height: 240.h,),
          Image.asset(imgurl,width: 480.w,),
          SizedBox(height: 48.h,),
          Container(
            child: Text(title,style: TextStyle(
              color: Colors.white,
              fontSize: 32.sp,
            ),),
          ),
          SizedBox(height: 24.h,),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 40.w),
            child: Text(content,style: TextStyle(
              color: Color.fromRGBO(255, 255, 255, 0.5),
              fontSize: 28.sp,
            ),textAlign: TextAlign.center,),
          ),
          SizedBox(height: 60.h,),
          GestureDetector(
            onTap: (){
              if(type == 1){
                _showNoDeviceAlert();
              }else if(type == 2){
                Navigator.of(context).pushNamed('DeviceNetPage');
              }
            },
            child: Container(
              width: 690.w,
              height: 90.w,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                  color: themeColor,
                  borderRadius: BorderRadius.circular(12.w)
              ),
              child: Text(btnTitle,style: TextStyle(
                  color: Colors.white,
                  fontSize: 32.sp
              ),),
            ),
          )
        ],
      ),
    );
  }

  Widget _topWidget(){
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 30.w),
      height: 44,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          GestureDetector(
            onTap: (){
              _showDeviceChoose();
              // eventBus.fire(IndexSliderChange(true));
              // setState(() {
              //   isopen = true;
              // });
              // if(drawerKey.currentState != null && drawerKey.currentState!.isDrawerOpen){
              //   drawerKey.currentState?.closeSlider();
              //   eventBus.fire(IndexSliderChange(false));
              //   setState(() {
              //     isopen = false;
              //   });
              // }else{
              // eventBus.fire(IndexSliderChange(true));
              // drawerKey.currentState?.openSlider();
              // setState(() {
              //   isopen = true;
              // });
              // }
            },
            child: Row(
              children: [
                // Container(
                //   child: Image.asset('assets/home/home_person.png',width: 64.w,),
                // ),
                // SizedBox(width: 20.w,),
                Visibility(visible: true,child: Container(
                  constraints: BoxConstraints(
                      maxWidth: 260.w
                  ),
                  child: Text(baseUserInfo['firstName']!=null? baseUserInfo['firstName']:baseUserInfo['nickname']??'user',style: TextStyle(
                      color: whiteFontColor,
                      fontSize: 40.sp,
                      fontWeight: FontWeight.w600
                  ),maxLines: 1,overflow: TextOverflow.ellipsis,textAlign: TextAlign.left,),
                ),),
                SizedBox(width: 6.w,),
                Image.asset('assets/common/control_down.png',width: 48.w,)
              ],
            ),
          ),
          Visibility(child: Container(
            child: Text(cndcValue.isNotEmpty && double.parse('${cndcValue ?? '0'}') <0?'Discharging'.tr: cndcValue.isNotEmpty && double.parse('${cndcValue ?? '0'}') >0?'Charging'.tr:'Ready'.tr,style: TextStyle(
                color:cndcValue.isNotEmpty && double.parse('${cndcValue ?? '0'}') <0? themeColor : Color(0xff06df85),
                fontSize: 28.sp,
                fontWeight: FontWeight.w600
            ),maxLines: 1,overflow: TextOverflow.ellipsis,),
          ),visible: baseDevice != null && baseDevice?['status'].toString() != '2',),
          // Container(
          //   child: Image.asset('assets/home/home_logo.png',height: 40.w,),
          // )
          // GestureDetector(
          //   onTap: (){
          //     JhPickerTool.showStringPicker(context,
          //         data: _list,
          //         clickCallBack: _selectType,
          //         normalIndex: 0);
          //   },
          //   child: Row(
          //     children: [
          //       SizedBox(width: 20.w,),
          //       Text(home_page_change_device_text.tr,style: TextStyle(
          //           color: fontColor
          //       ),)
          //     ],
          //   ),
          // ),
        ],
      ),
    );
  }

  _showDeviceChoose(){
    return SmartDialog.show(builder: (ctx){
      return TopDeviceSelect(
        callBack: (type){
          _getList(type: 1);
          if(type == 1){
            _showAddDeviceAlert();
          }
        },
      );
    },alignment: Alignment.topCenter,animationTime: Duration(milliseconds: 200));
  }

  _showAddDeviceAlert(){
    SmartDialog.show(builder: (ctx){
      return Container(
        height: 530.w,
        padding: EdgeInsets.only(left: 48.w,right: 48.w,top: 32.w,bottom: 48.w),
        decoration: BoxDecoration(
            color: Color(0xff1E1E1E),
            borderRadius: BorderRadius.only(topLeft: Radius.circular(32.w),topRight: Radius.circular(32.w))
        ),
        child: Column(
          children: [
            Container(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text('Add new device',style: TextStyle(
                      color: Colors.white,
                      fontSize: 28.sp,
                      fontWeight: FontWeight.w600
                  ),),
                  GestureDetector(
                    onTap: (){
                      SmartDialog.dismiss();
                    },
                    child: Icon(Icons.close,size: 30.sp,color: Color(0xff999999),),
                  )
                ],
              ),
            ),
            SizedBox(height: 100.w,),
            GestureDetector(
              onTap: (){
                SmartDialog.dismiss();
                getCameraPermission(context, (){
                  Navigator.of(context).pushNamed('ScanPage').then((val){
                    if(mounted){setState(() {});}
                  });
                });
              },
              child: Container(
                height: 108.w,
                width: 654.w,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    color: themeColor,
                    borderRadius: BorderRadius.circular(8.w)
                ),
                child:Text('Scan Your Device',style: TextStyle(
                    color: Colors.white,
                    fontSize: 28.sp,
                    fontWeight: FontWeight.w600
                ),),
              ),
            ),
            SizedBox(height: 32.w,),
            GestureDetector(
              onTap: (){
                SmartDialog.dismiss();
                Navigator.of(context).pushNamed('MinAddDeviceInput').then((val){
                  print('base===' + baseDevice.toString());
                  setState(() {});
                });
              },
              child: Container(
                height: 108.w,
                width: 654.w,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    color: Color(0xff2A2A2A),
                    borderRadius: BorderRadius.circular(8.w)
                ),
                child:Text('Manual Entry'.tr,style: TextStyle(
                    color: Colors.white,
                    fontSize: 28.sp,
                    fontWeight: FontWeight.w600
                ),),
              ),
            ),
          ],
        ),
      );
    },alignment: Alignment.bottomCenter);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: defaultBlackBgColor,

      body: GestureDetector(
        onTap: (){
        },
        child: baseDevices.isEmpty? _buildHomeNoData(1):
        baseDevice?['status'].toString() == '2'? _buildHomeNoData(2): Container(
          padding: EdgeInsets.symmetric(horizontal: 30.w),
          child: Column(
            children: [
              // Container(
              //   alignment: Alignment.center,
              //   child: TabBar(
              //     controller: _tabController,
              //     isScrollable: true,
              //     dividerHeight: 0,
              //     indicatorWeight: 2.w,
              //     labelPadding: EdgeInsets.only(right: 60.w),
              //     indicatorSize: TabBarIndicatorSize.label,
              //     padding: EdgeInsets.zero,
              //     tabAlignment: TabAlignment.start,
              //     indicator: BoxDecoration(
              //         borderRadius: BorderRadius.circular(2),
              //         color: themeColor
              //     ),
              //     indicatorPadding: EdgeInsets.only(left: 15.w, right: 15.w,top: 38,bottom: 7),
              //     labelStyle: TextStyle(
              //         color: themeColor,
              //         fontSize: 32.sp,
              //         fontWeight: FontWeight.w600
              //     ),
              //     unselectedLabelStyle: TextStyle(
              //         color: fontColor,
              //         fontSize: 32.sp,
              //         fontWeight: FontWeight.w600
              //     ),
              //     tabs: [
              //       Tab(text: monitor_detail_page_device_title.tr,),
              //       Tab(text: monitor_detail_page_waring_title.tr,),
              //     ],
              //   ),
              // ),
              SizedBox(height: ScreenUtil().statusBarHeight+0.w),
              _topWidget(),
              Expanded(
                  child: Container(
                    margin: EdgeInsets.only(top: 15.w),
                    child: Column(
                      children: [
                        Expanded(child: _buildMonitor()),
                        // GestureDetector(
                        //   onTap: (){
                        //     Navigator.of(context).pushNamed('DeviceControl');
                        //   },
                        //   child: Container(
                        //     alignment: Alignment.center,
                        //     height: 108.w,
                        //     margin: EdgeInsets.only(bottom: 24.w),
                        //     decoration: BoxDecoration(
                        //       color: themeColor,
                        //       borderRadius: BorderRadius.circular(16.w)
                        //     ),
                        //     child: Text('Setting',style: TextStyle(
                        //       color: Colors.white,
                        //       fontWeight: FontWeight.w600,
                        //       fontSize: 32.sp
                        //     ),),
                        //   ),
                        // )
                      ],
                    ),
                    // TabBarView(
                    //   controller: _tabController,
                    //   children: [
                    //     _buildMonitor(),
                    //     _buildWaring()
                    //   ],
                    // ),
                  )
              )
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildMonitor() {
    return Container(
      color: Colors.transparent,
      child: EasyRefresh(
        header: myRefreshHeader(),
        onRefresh: ()async{
          _getList();
        },
        child:isDone? SingleChildScrollView(
          child: Column(
            children: [
              SizedBox(height: 40.w,),
              Container(
                decoration: BoxDecoration(
                    color: Color(0xff1e1d20),
                  borderRadius: BorderRadius.circular(24.w),
                  border: Border.all(color: Colors.white.withAlpha(30))
                ),
                padding: EdgeInsets.all(30.w),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      child: Column(
                        children: [
                          Row(
                            children: [
                              // Text('Device SN',style: TextStyle(
                              //     fontWeight: FontWeight.w600,
                              //     color: Color(0xff999999),
                              //     fontSize: 28.sp
                              // ),),
                              Expanded(child: Text(baseDeviceNum??'',style: TextStyle(
                                  fontWeight: FontWeight.w600,
                                  color: Colors.white,
                                  fontSize: 28.sp
                              ),textAlign: TextAlign.left,)),
                            ],
                          ),
                          SizedBox(height: 34.w,),
                          Container(
                            height: 1.w,
                            color: Color(0x2fEEEEEE),
                          )
                        ],
                      ),
                    ),
                    SizedBox(height: 32.w,),
                    Container(
                      height: 50.w,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: _buildStatusWidget(),
                      ),
                    )
                  ],
                ),
              ),
              // SizedBox(height: 32.w,),
              // Container(
              //   decoration: BoxDecoration(
              //       color: Color(0xff222222),
              //       borderRadius: BorderRadius.circular(32.w)
              //   ),
              //   padding: EdgeInsets.all(30.w),
              //   child: Column(
              //     crossAxisAlignment: CrossAxisAlignment.start,
              //     children: [
              //       Container(
              //         child: Column(
              //           children: [
              //             Row(
              //               children: [
              //                 Text(monitor_detail_page_nbqinfo_title.tr,style: TextStyle(
              //                     fontWeight: FontWeight.w600,
              //                     color: Colors.white,
              //                     fontSize: 28.sp
              //                 ),),
              //               ],
              //             ),
              //             SizedBox(height: 34.w,),
              //             Container(
              //               height: 1.w,
              //               color: Color(0x2fEEEEEE),
              //             )
              //           ],
              //         ),
              //       ),
              //       SizedBox(height: 34.w,),
              //       ListView.builder(itemBuilder: (context,index){
              //         Map item = _infos[index];
              //         return GestureDetector(
              //           onTap: (){
              //             showModalBottomSheet(context: context, builder: (ctx){
              //               return Container(
              //                 decoration: BoxDecoration(
              //                     color: Color(0xff222222),
              //                     borderRadius: BorderRadius.circular(40.w)
              //                 ),
              //                 padding: EdgeInsets.only(left: 40.w,right: 40.w,bottom: ScreenUtil().bottomBarHeight+80.w,top:40.w),
              //                 child: Column(
              //                   mainAxisSize: MainAxisSize.min,
              //                   children: [
              //                     Row(
              //                       crossAxisAlignment: CrossAxisAlignment.start,
              //                       children: [
              //                         Container(
              //                           constraints: BoxConstraints(
              //                               maxWidth: 400.w,
              //                               minWidth: 300.w
              //                           ),
              //                           child: Text(item['name'],style: TextStyle(
              //                               fontWeight: FontWeight.w600,
              //                               color: Color(0xff999999),
              //                               fontSize: 24.sp
              //                           ),),
              //                         ),
              //                         SizedBox(width: 20.w,),
              //                         Expanded(child: Container(
              //                           alignment: Alignment.centerRight,
              //                           child: RichText(
              //                             text: TextSpan(
              //                                 children: [
              //                                   TextSpan(
              //                                     text: '${item['value']??''}',style: TextStyle(
              //                                       fontWeight: FontWeight.w600,
              //                                       color: Colors.white,
              //                                       fontSize: 28.sp
              //                                   ),
              //                                   ),
              //                                   TextSpan(
              //                                     text: '${item['unit']??''}',style: TextStyle(
              //                                       fontWeight: FontWeight.w600,
              //                                       color: Color(0xff999999),
              //                                       fontSize: 24.sp
              //                                   ),
              //                                   )
              //                                 ]
              //                             ),
              //                           ),
              //                         )),
              //                       ],
              //                     )
              //                   ],
              //                 ),
              //               );
              //             },isScrollControlled: true,backgroundColor: Color(0xff222222));
              //           },
              //           child: Container(
              //             color: Colors.transparent,
              //             padding: EdgeInsets.only(bottom: 40.w),
              //             child: Row(
              //               crossAxisAlignment: CrossAxisAlignment.start,
              //               children: [
              //                 Container(
              //                   constraints: BoxConstraints(
              //                       maxWidth: 400.w,
              //                       minWidth: 300.w
              //                   ),
              //                   child: Text(item['name'],style: TextStyle(
              //                       fontWeight: FontWeight.w600,
              //                       color: Color(0xff999999),
              //                       fontSize: 24.sp
              //                   ),maxLines: 1,overflow: TextOverflow.ellipsis,),
              //                 ),
              //                 SizedBox(width: 20.w,),
              //                 Expanded(child:  GestureDetector(
              //                   child: Container(
              //                     alignment: Alignment.centerRight,
              //                     child: RichText(
              //                       text: TextSpan(
              //                           children: [
              //                             TextSpan(
              //                               text: '${item['value']??''}',style: TextStyle(
              //                                 fontWeight: FontWeight.w600,
              //                                 color: Colors.white,
              //                                 fontSize: 28.sp
              //                             ),
              //                             ),
              //                             TextSpan(
              //                               text: '${item['unit']??''}',style: TextStyle(
              //                                 fontWeight: FontWeight.w600,
              //                                 color: Color(0xff999999),
              //                                 fontSize: 24.sp
              //                             ),
              //                             )
              //                           ]
              //                       ),
              //                       maxLines: 1,
              //                       overflow: TextOverflow.ellipsis,
              //                     ),
              //                   ),
              //                 )),
              //               ],
              //             ),
              //           ),
              //         );
              //       },padding: EdgeInsets.zero,itemCount: _infos.length,physics: NeverScrollableScrollPhysics(),shrinkWrap: true,),
              //     ],
              //   ),
              // ),
              SizedBox(height: 32.w,),
              Visibility(visible: _newDatas.isNotEmpty,child: Container(
                decoration: BoxDecoration(
                    color: Color(0xff1e1d20),
                    borderRadius: BorderRadius.circular(24.w)
                ),
                padding: EdgeInsets.all(30.w),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      child: Column(
                        children: [
                          Row(
                            children: [
                              Expanded(child: Text(monitor_detail_page_data_title.tr,style: TextStyle(
                                  fontWeight: FontWeight.w600,
                                  color: Colors.white,
                                  fontSize: 28.sp
                              ),)),
                              GestureDetector(
                                onTap: (){
                                  Navigator.of(context).pushNamed('DeviceControl');
                                },
                                child: Container(
                                  width: 160.w,
                                  height: 60.w,
                                  alignment: Alignment.center,
                                  decoration: BoxDecoration(
                                    border: Border.all(color: Colors.white),
                                    borderRadius: BorderRadius.circular(8.w)
                                  ),
                                  child: Text('Settings'.tr,style: TextStyle(
                                    color: Colors.white,
                                    fontSize: 28.sp,
                                    fontWeight: FontWeight.w600
                                  ),),
                                ),
                              )
                            ],
                          ),
                          SizedBox(height: 34.w,),
                          Container(
                            height: 1.w,
                            color: Color(0x2fEEEEEE),
                          )
                        ],
                      ),
                    ),
                    SizedBox(height: 34.w,),
                    ListView.builder(itemBuilder: (context,index){
                      Map item = _newDatas[index];
                      if((item['name']??'').toString().isEmpty){
                        return Container();
                      }
                      return GestureDetector(
                        onTap: (){
                          showModalBottomSheet(context: context, builder: (ctx){
                            return Container(
                              decoration: BoxDecoration(
                                  color: Color(0xff222222),
                                  borderRadius: BorderRadius.circular(40.w)
                              ),
                              padding: EdgeInsets.only(left: 40.w,right: 40.w,bottom: ScreenUtil().bottomBarHeight+80.w,top:40.w),
                              child: Column(
                                mainAxisSize: MainAxisSize.min,
                                children: [
                                  Row(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      Container(
                                        constraints: BoxConstraints(
                                            maxWidth: 400.w,
                                            minWidth: 300.w
                                        ),
                                        child: Text(item['name'],style: TextStyle(
                                            fontWeight: FontWeight.w600,
                                            color: Color(0xff999999),
                                            fontSize: 24.sp
                                        ),),
                                      ),
                                      SizedBox(width: 20.w,),
                                      Expanded(child: Container(
                                        alignment: Alignment.centerRight,
                                        child: RichText(
                                          text: TextSpan(
                                              children: [
                                                TextSpan(
                                                  text: '${item['value']??''}',style: TextStyle(
                                                    fontWeight: FontWeight.w600,
                                                    color: Colors.white,
                                                    fontSize: 28.sp
                                                ),
                                                ),
                                                TextSpan(
                                                  text: '${item['unit']??''}',style: TextStyle(
                                                    fontWeight: FontWeight.w600,
                                                    color: Color(0xff999999),
                                                    fontSize: 24.sp
                                                ),
                                                )
                                              ]
                                          ),
                                        ),
                                      )),
                                    ],
                                  )
                                ],
                              ),
                            );
                          },isScrollControlled: true,backgroundColor: Color(0xff222222));
                        },
                        child: Container(
                          color: Colors.transparent,
                          margin: EdgeInsets.only(bottom: 40.w),
                          child: Row(
                            children: [
                              Container(
                                constraints: BoxConstraints(
                                    maxWidth: 400.w,
                                    minWidth: 300.w
                                ),
                                child: Text(item['name']??'',style: TextStyle(
                                    fontWeight: FontWeight.w600,
                                    color: Color(0xff999999),
                                    fontSize: 24.sp
                                ),maxLines: 1,overflow: TextOverflow.ellipsis),
                              ),
                              SizedBox(width: 32.w,),
                              Expanded(child: GestureDetector(
                                child: Container(
                                  alignment: Alignment.centerRight,
                                  child: RichText(
                                    text: TextSpan(
                                        children: [
                                          TextSpan(
                                            text: '${item['value']??''}',style: TextStyle(
                                              fontWeight: FontWeight.w600,
                                              color: Colors.white,
                                              fontSize: 28.sp
                                          ),
                                          ),
                                          TextSpan(
                                            text: '${item['unit']??''}',style: TextStyle(
                                              fontWeight: FontWeight.w600,
                                              color: Color(0xff999999),
                                              fontSize: 24.sp
                                          ),
                                          )
                                        ]
                                    ),
                                    maxLines: 1,
                                    overflow: TextOverflow.ellipsis,
                                  ),
                                ),
                              )
                              ),
                            ],
                          ),
                        ),
                      );
                    },padding: EdgeInsets.zero,itemCount: _newDatas.length,physics: NeverScrollableScrollPhysics(),shrinkWrap: true,)
                  ],
                ),
              ),),
              SizedBox(height: ScreenUtil().bottomBarHeight+30.w,)
            ],
          ),
        ):Container(),
      ),
    );
  }

  List<Widget> _buildStatusWidget(){
    List<Widget> widgets = [];
    for (var item in _status) {
      widgets.add(GestureDetector(
        onTap: (){
          showModalBottomSheet(context: context, builder: (ctx){
            return Container(
              decoration: BoxDecoration(
                  color: Color(0xff222222),
                  borderRadius: BorderRadius.circular(40.w)
              ),
              padding: EdgeInsets.only(left: 40.w,right: 40.w,bottom: ScreenUtil().bottomBarHeight+80.w,top:40.w),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Container(
                    alignment: Alignment.centerLeft,
                    color: Colors.transparent,
                    child: Text(item['name'],style: TextStyle(
                        fontWeight: FontWeight.w600,
                        color: Color(0xff999999),
                        fontSize: 24.sp
                    ),),
                  ),
                  SizedBox(height:16.w),
                  Container(
                    alignment: Alignment.centerLeft,
                    child: RichText(
                      text: TextSpan(
                          children: [
                            TextSpan(
                              text: '${item['desc']??''}',style: TextStyle(
                                fontWeight: FontWeight.w600,
                                color: Colors.white,
                                fontSize: 28.sp
                            ),
                            ),
                            TextSpan(
                              text: '${item['unit']??''}',style: TextStyle(
                                fontWeight: FontWeight.w600,
                                color: Color(0xff999999),
                                fontSize: 24.sp
                            ),
                            )
                          ]
                      ),
                    ),
                  )
                ],
              ),
            );
          },isScrollControlled: true,backgroundColor: Color(0xff222222));
        },
        child: Container(
          height: 60.w,
          child: Row(
            children: [
              Container(
                height: 10.w,
                width: 10.w,
                decoration: BoxDecoration(
                    color: item['status'].toString() == '1'? Color(0xff36E039):Color(0xffFF5353),
                    borderRadius: BorderRadius.circular(20.w)
                ),
              ),
              SizedBox(width: 10.w,),
              Container(
                color: Colors.transparent,
                child: Row(
                  children: [
                    Text(item['name'],style: TextStyle(
                        fontWeight: FontWeight.w600,
                        color: Color(0xff999999),
                        fontSize: 24.sp
                    ),),
                    SizedBox(width: 10.w,),
                    // Icon(Icons.info,color: Color(0xff999999),size: 24.sp,),
                  ],
                ),
              ),
            ],
          ),
        ),
      ));
    }
    return widgets;
  }

  Widget _buildWaring() {
    return Container(
      child: EasyRefresh(
        header: myRefreshHeader(),
        onRefresh: ()async{
          _getList();
        },
        child: SingleChildScrollView(
          child: Column(
            children: [
              GestureDetector(
                onTap: (){
                  SmartDialog.show(builder: (context) {
                    return InverterDetailDialog();
                  },alignment: Alignment.bottomCenter,clickMaskDismiss: true);
                },
                child: Container(
                  alignment: Alignment.centerLeft,
                  child: Text('筛选'),
                ),
              ),
              SizedBox(height: 30.w,),
              Container(
                decoration: BoxDecoration(
                    border: Border.all(color: Colors.grey,width: 1)
                ),
                padding: EdgeInsets.all(30.w),
                child: Column(
                  children: [
                    ListView.builder(itemBuilder: (context,index){
                      return Container(
                        margin: EdgeInsets.only(bottom: 20.w),
                        child: Row(
                          children: [
                            Expanded(child: Container(
                              child: Column(
                                children: [
                                  Text('异常类型'),
                                  Text('发生事件：2024-09-15 10：22：36'),
                                  Text('解除时间：2024-09-15 10：22：36'),
                                  Text('描述'),
                                ],
                                mainAxisAlignment: MainAxisAlignment.start,
                                crossAxisAlignment: CrossAxisAlignment.start,
                              ),
                            )),
                            SizedBox(width: 20.w,),
                            Container(
                              child: Text('发生中'),
                            ),
                          ],
                        ),
                      );
                    },padding: EdgeInsets.zero,itemCount: 20,physics: NeverScrollableScrollPhysics(),shrinkWrap: true,)
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

}
